﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Urd.Cemetery
{
    // public List<packet> recieved;
    // public Dictionary<IPAddress, List<packet>> clients;
    /*shit
    static void top_manager_process(object o)
    {
            
        yoba_manager Manager = (yoba_manager)o; 

        System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        customCulture.NumberFormat.NumberDecimalSeparator = ".";
        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
            
            while (Manager.Working)
            {

                Thread.Sleep(20);
                if (Manager.Recieved.Count > 0)
                {
                    lock (Manager.Recieved)
                    {
                        lock (Manager.Awaiting)
                        {
                            //sqlconnector.Sql("begin transaction",false);
                            while (Manager.Recieved.Count>0)
                            {

                                packet p = Manager.Recieved[0];
                                Manager.Recieved.Remove(p);

                                if (p != null)
                                {

                                     


                                    if (p.command == Command.Get)
                                    {
                                        packet newpacket = new packet(Opcode.ManagerInfo);

                                        object[] temp = Manager.Cache.Sql("select address, port from managers where opcode=" + p.GetInt(0).ToString(),true);

                                        string tempstring="";
                                        foreach (object t in temp) 
                                        {
                                            tempstring += o.ToString() + " ";
                                        }

                                        newpacket.Push(tempstring);
                                        Manager.Awaiting.Add(newpacket);
                                    }
                                     
                                }
                            }
                        }//sqlconnector.Sql("commit transaction",false);
                    }
                }
            }
            
    }
    static void login_manager_process(object o)
    {
        yoba_manager Manager = (yoba_manager)o;

        System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        customCulture.NumberFormat.NumberDecimalSeparator = ".";
        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

        while (Manager.Working)
        {

            Thread.Sleep(20);
            if (Manager.Recieved.Count > 0)
            {
                lock (Manager.Recieved)
                {
                    lock (Manager.Awaiting)
                    {
                        //sqlconnector.Sql("begin transaction",false);
                        while (Manager.Recieved.Count > 0)
                        {

                            packet p = Manager.Recieved[0];
                            Manager.Recieved.Remove(p);

                            if (p != null)
                            {
                                if (p.command == Command.Login)
                                {
                                    string[] data = p.Someinfo.Split('|');
                                    debug.Log(p.Someinfo, debug.loglevel.Errors);
                                    Manager.Awaiting.Add(new packet(Opcode.OK));

                                }
                            }
                        }
                    }//sqlconnector.Sql("commit transaction",false);
                }
            }
        }
    }
    static void posmanager_process(object o)
    {
        yoba_manager Manager = (yoba_manager)o;

        System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        customCulture.NumberFormat.NumberDecimalSeparator = ".";
        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

        while (Manager.Working)
        {

            Thread.Sleep(20);
            if (Manager.Recieved.Count > 0)
            {
                lock (Manager.Recieved)
                {
                    lock (Manager.Awaiting)
                    {
                        //sqlconnector.Sql("begin transaction",false);
                        while (Manager.Recieved.Count > 0)
                        {

                            packet p = Manager.Recieved[0];
                            Manager.Recieved.Remove(p);

                            if (p != null)
                            {
                                if (p.command == Command.Get)
                                {
                                    packet newpacket = new packet(Opcode.Pos);

                                    object[] temp = Manager.Cache.Sql("select id, x, y, z from position where id=" + p.GetInt(0).ToString(), true);

                                    if (temp.Length == 4 && temp[0] != null)
                                    {


                                        newpacket.Push((int)temp[0]);
                                        newpacket.Push(Convert.ToSingle(temp[1]));
                                        newpacket.Push(Convert.ToSingle(temp[2]));
                                        newpacket.Push(Convert.ToSingle(temp[3]));
                                        Manager.Awaiting.Add(newpacket);
                                    }
                                    else
                                    {
                                        newpacket = new packet(Opcode.Error);
                                        newpacket.Push(p.ID);
                                        Manager.Awaiting.Add(newpacket);
                                    }

                                }
                                if (p.command == Command.Set)
                                {
                                    packet newpacket = new packet(Opcode.Pos);

                                    object[] temp =
                                        Manager.Cache.Sql
                                        ("update position set " +
                                        "x=" + p.GetFloat(1) + ", " +
                                        "y=" + p.GetFloat(2) + ", " +
                                        "z=" + p.GetFloat(3) + " " +
                                        "where id=" + p.GetInt(0).ToString(), false);

                                    if (temp.Length == 1 && temp[0] != null)
                                    {


                                        newpacket.Push(p.ID);
                                        newpacket.Push(Convert.ToSingle(temp[0]));
                                        newpacket.command = Command.SetOK;
                                        Manager.Awaiting.Add(newpacket);
                                    }
                                    else
                                    {
                                        newpacket = new packet(Opcode.Pos, Command.SetError);
                                        newpacket.Push(p.ID);
                                        Manager.Awaiting.Add(newpacket);
                                    }

                                }

                            }
                        }
                    }//sqlconnector.Sql("commit transaction",false);
                }
            }
        }
    }
    */
    /*       private  packet pos_process(packet p, cache c)
         {
             DateTime timestart = DateTime.Now;            
             TimeSpan timespan;
             if (p != null)
             {
                 packet newpacket = new packet(Opcode.Pos);

                 switch (p.command)
                 {
                        
                     #region near
                     case Command.GetNear:

                         //id - id
                         // distance - distance

                         float dist = 8f;
                         object[] tempn = c.Sql("select id, x, y, z from position where id=" + p.GetInt(0).ToString(), true);
                         if (tempn.Length == 4 && tempn[0] != null)
                         {
                             float x = Convert.ToSingle(tempn[1]);
                             float y = Convert.ToSingle(tempn[2]);
                             float z = Convert.ToSingle(tempn[3]);



                             object[] temp2 = c.Sql(
                                 "create table near as select id from position where"
                                 + " abs(x)-abs(" + x.ToString() + ")<" + dist.ToString() + " and "
                                 + " abs(y)-abs(" + y.ToString() + ")<" + dist.ToString() + " and "
                                 + " abs(x)-abs(" + z.ToString() + ")<" + dist.ToString()
                                 , true);
                             //System.Data.DataSet ds = new System.Data.DataSet("anus");

                             //System.Data.SQLite.SQLiteDataAdapter da = new System.Data.SQLite.SQLiteDataAdapter();
                             //da.Fill(ds);
                             //ds.Tables[0].Rows[0].


                             temp2 = c.Sql("select * from near", true);

                             for (int i = 1; i < temp2.Length; i++) 
                             {
                                 newpacket.Push((int)temp2[i]);
                             }
                             c.Sql("drop table near", true);
                             timespan = DateTime.Now - timestart;
                             debug.Log("[pos][getnear]"+timespan.ToString(),debug.loglevel.Status);
                            
                             return newpacket;
                         }
                         else return new packet(Opcode.Error, Command.GetNear);

                     #endregion
                     #region get
                     case Command.Get:

                         //packet newpacket = new packet(Opcode.Pos);

                         object[] tempg = c.Sql("select id, x, y, z from position where id=" + p.GetInt(0).ToString(), true);

                         if (tempg.Length == 4 && tempg[0] != null)
                         {
                             newpacket.Push((int)tempg[0]);
                             debug.Log(tempg[0].ToString(), debug.loglevel.Errors);
                             newpacket.Push(Convert.ToSingle(tempg[1]));
                             newpacket.Push(Convert.ToSingle(tempg[2]));
                             newpacket.Push(Convert.ToSingle(tempg[3]));
                             timespan = DateTime.Now - timestart;
                             debug.Log("[pos][get]" + timespan.ToString(), debug.loglevel.Status);
                             return newpacket;
                         }
                         else
                         {
                             newpacket = new packet(Opcode.Error);
                             newpacket.Push(p.ID);
                             return newpacket;
                         }
                     // break;
                     #endregion
                     #region set
                     case Command.Set:



                         object[] temps =
                             c.Sql
                             ("update position set " +
                             "x=" + p.GetFloat(1) + ", " +
                             "y=" + p.GetFloat(2) + ", " +
                             "z=" + p.GetFloat(3) + " " +
                             "where id=" + p.GetInt(0).ToString(), false);

                         if (temps.Length == 1 && temps[0] != null)
                         {


                             newpacket.Push(p.ID);
                             newpacket.Push(Convert.ToSingle(temps[0]));
                             newpacket.command = Command.SetOK;
                             return newpacket;
                         }
                         else
                         {
                             newpacket = new packet(Opcode.Pos, Command.SetError);
                             newpacket.Push(p.ID);
                             return newpacket;
                         }


                     #endregion
                     #region new
                     case Command.New:
                         float xn = p.GetFloat(1);                        
                         float yn = p.GetFloat(2);
                         float zn = p.GetFloat(3);

                         object[] tempnew = c.Sql("select min(id)-1 from position",true);
                         int min = 0;
                            min =Convert.ToInt32(tempnew[0]);
                         tempnew = c.Sql
                             ("insert into position values("
                             +min+","+xn+","+yn+","+zn+")",false);
                        
                             newpacket.Push(min);

                         return newpacket;
                     #endregion
                     default:
                         return new packet(Opcode.Error, Command.WrongManager);

                 }
             }
             else return new packet(Opcode.Error, Command.None);
         }
         */
}
